'''
Created on 12/02/2010

@author: eh01
'''
import urllib
import re
import urllib2
import httplib

post_data = '''<ns0:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/">
   <ns0:Header>
      <ns1:To xmlns:ns1="http://www.w3.org/2005/08/addressing">https://edais.daf.gov.au/ode/processes/TestCase_CR-BTV1000R.2.3.0r2_CreateReferralInitiator</ns1:To>
      <ns1:Action xmlns:ns1="http://www.w3.org/2005/08/addressing">http://example.xml.gov.au/CreateReferral_Initiator.2.3.0r2/Accept</ns1:Action>
   </ns0:Header>
   <ns0:Body>
      <ns1:AcceptCreateReferralTransaction xmlns:ns1="http://xml.gov.au/edais/bm/AcceptCreateReferralTransaction.msg.2.3.0r2">
         <ns1:Application>
            <ns2: xmlns:ns2="http://xml.gov.au/edais/core/da.data.2.3.0r2">test_element_string</ns2:ApplicationNumber>
         </ns1:Application>
         <ns1:StandardBusinessMessageHeader>
            <ns2:BusinessScope xmlns:ns2="http://xml.gov.au/edais/core/msg.data.2.3.0r2">
               <ns2:Type>BusinessCollaboration</ns2:Type>
            </ns2:BusinessScope>
            <ns2:DocumentIdentification xmlns:ns2="http://xml.gov.au/edais/core/msg.data.2.3.0r2">
               <ns2:CreationDateTime>2002-05-30T09:30:10Z</ns2:CreationDateTime>
               <ns2:InstanceID>test_element_string</ns2:InstanceID>
               <ns2:Standard>eDAIS</ns2:Standard>
               <ns2:Type>test_element_string</ns2:Type>
               <ns2:TypeVersion>test_element_string</ns2:TypeVersion>
            </ns2:DocumentIdentification>
            <ns2:ReceiverPartner xmlns:ns2="http://xml.gov.au/edais/core/msg.data.2.3.0r2">
               <ns2:Authority>test_element_string</ns2:Authority>
               <ns2:ID>test_element_string</ns2:ID>
               <ns2:ProcessRole>test_element_string</ns2:ProcessRole>
            </ns2:ReceiverPartner>
            <ns2:SenderPartner xmlns:ns2="http://xml.gov.au/edais/core/msg.data.2.3.0r2">
               <ns2:Authority>test_element_string</ns2:Authority>
               <ns2:ID>test_element_string</ns2:ID>
               <ns2:ProcessRole>test_element_string</ns2:ProcessRole>
            </ns2:SenderPartner>
            <ns2:Version xmlns:ns2="http://xml.gov.au/edais/core/msg.data.2.3.0r2">test_element_string</ns2:Version>
         </ns1:StandardBusinessMessageHeader>
      </ns1:AcceptCreateReferralTransaction>
   </ns0:Body>
</ns0:Envelope>'''

opener = urllib2.build_opener(urllib2.HTTPSHandler)
url_pattern = re.compile(r'https?://(.*?)/(.*)')

#def send_request(request_url, request_data, http_headers):
#    request = urllib2.Request(request_url, data=request_data)
#    if http_headers:
#        for key, value in http_headers.items():
#            request.add_header(key, value)    
##    response = opener.open(request)
#    response = urllib2.urlopen(request)
#    response_xml = response.read()
#    code = response.code
#    return (code, response_xml)

#def send_request(request_url, request_data, http_headers):
#    m = url_pattern.match(request_url)
#    host = m.group(1)
#    path = m.group(2)
#    c = httplib.HTTPConnection(host)
#    c.request(method="POST", url="/" + path, body=request_data, headers=http_headers)
#    c.set_debuglevel(1)
#    response = c.getresponse()
##    print response.status, response.reason
#    data = response.read()
#    return (response.status, data)

def send_request(request_url, request_data, http_headers):
    m = url_pattern.match(request_url)
    host = m.group(1)
    path = m.group(2)
    c = httplib.HTTPSConnection(host)
    c.request(method="POST", url="/" + path, body=request_data, headers=http_headers)
    c.set_debuglevel(1000)
    response = c.getresponse()
#    print response.status, response.reason
    data = response.read()
    return (response.status, data)

if __name__ == '__main__':
    m = url_pattern.match('https://edais.daf.gov.au/ode/processes/TestCase_CR-BTV1000R.2.3.0r2_CreateReferralInitiator')
    host = m.group(1)
    path = m.group(2)
    print host
    print path
#    c = httplib.HTTPSConnection("www.landexchange.vic.gov.au")
#    c.request("GET", "/spear/login/Login.do")
    c = httplib.HTTPSConnection("edais.daf.gov.au")
    c.request(method="POST", url="/ode/processes/TestCase_CR-BTV1000R.2.3.0r2_CreateReferralInitiator/Accept", 
              body=post_data, headers={'SOAPAction': 'https://edais.daf.gov.au/CreateReferral_Initiator.2.3.0r2/Accept', 'Content-Type': 'text/xml;charset=UTF-8'})
    c.set_debuglevel(1)
    response = c.getresponse()
    print response.status, response.reason
    data = response.read()
    print data
    
#    request = urllib2.Request('https://edais.daf.gov.au/ode/processes/TestCase_CR-BTV1000R.2.3.0r2_CreateReferralInitiator/Accept')
#    response = urllib2.urlopen(request)
#    response_xml = response.read()
#    code = response.code
#    print (code, response_xml)